package com.green.javagreen.service;


import com.green.javagreen.bean.StudentUserBean;
import com.green.javagreen.util.ExampleUtil;
import com.green.javagreen.util.IntegerUtil;
import com.green.javagreen.util.TextUtil;
import com.green.javagreen.util.UpdateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

/**
 * email:brioal@foxmail.com
 * github:https://github.com/Brioal
 */
@Service
public class StudentUserServiceImpl implements StudentUserService {
    private StudentUserRepository studentUserRepository;

    @Autowired
    public void setStudentUserRepository(StudentUserRepository studentUserRepository) {
        this.studentUserRepository = studentUserRepository;
    }

    @Override
    public StudentUserBean save(StudentUserBean bean) {
        StudentUserBean exitBean = get(bean.getId());
        if (exitBean == null) {
            exitBean = bean;
        } else {
            UpdateTool.copyNullProperties(bean, exitBean);
        }
        studentUserRepository.save(exitBean);
        return exitBean;
    }

    @Override
    public StudentUserBean get(Integer id) {
        if (IntegerUtil.isNotValid(id)) {
            return null;
        }
        return studentUserRepository.findById(id).orElse(null);
    }

    @Override
    public StudentUserBean get(StudentUserBean bean) {
        return studentUserRepository.findOne(ExampleUtil.getSimpleExample(bean)).orElse(null);
    }

    @Override
    public Page<StudentUserBean> find(StudentUserBean bean) {
        return studentUserRepository.findAll(ExampleUtil.getSimpleExample(bean), bean.getPageRequest());
    }

    @Override
    public void delete(Integer id) {
        if (IntegerUtil.isNotValid(id)) {
            return;
        }
        studentUserRepository.deleteById(id);
    }

    @Override
    public StudentUserBean findByCode(String code) {
        if (TextUtil.isNotValid(code)) {
            return null;
        }
        return studentUserRepository.findByCodeEquals(code);
    }
}
